package com.xinruke.hostels.support.controller;

import com.xinruke.hostels.common.controller.BaseController;
import com.xinruke.hostels.common.po.XtZidiancanshu;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.OrderField;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.support.common.cache.PushQueueUtil;
import com.xinruke.hostels.support.interfaces.dto.*;
import com.xinruke.hostels.support.service.SystemDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SystemDictController
 * @CreateDate 2019/6/16
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 系统字典
 */
@RestController
@RequestMapping("/system/dict")
@Api(tags = "系统字典")
public class SystemDictController extends BaseController {

    @Autowired
    private SystemDictService systemDictService;

    /**
     * 获取系统字典集合
     *
     * @param systemDictQueryDTO
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "获取系统字典集合")
    public ResultVO<List<SystemDictQueryResultDTO>> getSystemDictList(@RequestBody @Valid SystemDictQueryDTO systemDictQueryDTO) {
        ResultVO<List<SystemDictQueryResultDTO>> resultVO = ResultVO.fail();

        //拼装查询条件
        systemDictQueryDTO.getQueryConditions().addCondition("shanchubiaoshi", EConditionOperator.EQULS, XtZidiancanshu.SHANCHUBIAOSHI_BUSHANCHU);
        systemDictQueryDTO.getQueryConditions().addCondition("fenzubianma", EConditionOperator.EQULS, systemDictQueryDTO.getFenzubianma());
        systemDictQueryDTO.getQueryConditions().addCondition("canshumingcheng", EConditionOperator.EQULS, systemDictQueryDTO.getCanshumingcheng());
        systemDictQueryDTO.getQueryConditions().addCondition("canshuzhi", EConditionOperator.EQULS, systemDictQueryDTO.getCanshuzhi());

        //拼装排序字段
        List<OrderField> orderFieldList = new ArrayList<>();
        OrderField orderField = new OrderField("zhanshishunxu", false);
        orderFieldList.add(orderField);
        systemDictQueryDTO.setOrderFields(orderFieldList);

        //获取字典集合
        List<XtZidiancanshu> list = systemDictService.getSystemDictList(systemDictQueryDTO);

        if (!CollectionUtils.isEmpty(list)) {
            List<SystemDictQueryResultDTO> systemDictVOList = new ArrayList<>();
            for (XtZidiancanshu zidiancanshu : list) {
                SystemDictQueryResultDTO systemDictQueryResultDTO = new SystemDictQueryResultDTO();
                BeanUtils.copyProperties(zidiancanshu, systemDictQueryResultDTO);
                systemDictVOList.add(systemDictQueryResultDTO);
            }

            resultVO = ResultVO.success();
            resultVO.setData(systemDictVOList);
        }

        return resultVO;
    }

    /**
     * 获取系统字典集合
     *
     * @param systemDictQueryDTO
     * @return
     */
    @PostMapping("/group/list")
    @ApiOperation(value = "全部分组集合")
    public ResultVO<RowsDataVO<SystemDictGroupQueryResultDTO>> getSystemDictGroupList(@RequestBody @Valid SystemDictGroupQueryDTO systemDictQueryDTO) {
        return systemDictService.getSystemDictGroupList(systemDictQueryDTO);
    }

    /**
     * 获取全部字典参数
     *
     * @param systemDictAllQueryDTO
     * @return
     */
    @PostMapping("/all/list")
    @ApiOperation(value = "获取全部字典参数")
    public ResultVO<List<SystemDictAllQueryResultDTO>> getAllDict(@RequestBody @Valid SystemDictAllQueryDTO systemDictAllQueryDTO) {
        return systemDictService.getAllDict(systemDictAllQueryDTO);
    }

    /**
     * @param systemDictAddQueryDTO
     * @return
     */
    @PostMapping("param/add")
    @ApiOperation(value = "添加字典参数")
    public ResultVO addParam(@RequestBody @Valid SystemDictAddQueryDTO systemDictAddQueryDTO) {
        return systemDictService.addParam(systemDictAddQueryDTO);
    }

    /**
     * @param systemDictAddQueryDTO
     * @return
     */
    @PostMapping("param/modify")
    @ApiOperation(value = "修改字典参数")
    public ResultVO ModifyParam(@RequestBody @Valid SystemDictModifyQueryDTO systemDictAddQueryDTO) {
        return systemDictService.modifyParam(systemDictAddQueryDTO);
    }

    /**
     * @param dictDelQueryDTO
     * @return
     */
    @PostMapping("param/del")
    @ApiOperation(value = "删除字典参数")
    public ResultVO delParam(@RequestBody @Valid SystemDictDelQueryDTO dictDelQueryDTO) {
        return systemDictService.del(dictDelQueryDTO);
    }

    /**
     * 获取系统字典集合
     *
     * @param fenzubianma
     * @param canshuzhi
     * @return
     */
    @GetMapping("/{fenzubianma}/{canshuzhi}")
    @ApiOperation(value = "获取系统字典名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fenzubianma", value = "分组编码", paramType = "path", required = true),
            @ApiImplicitParam(name = "canshuzhi", value = "参数值", paramType = "path", required = true)
    })
    public String getSystemDictName(@PathVariable String fenzubianma, @PathVariable String canshuzhi) {
        String result = "";

        //拼装查询条件
        SystemDictQueryDTO systemDictQueryDTO = new SystemDictQueryDTO();

        systemDictQueryDTO.getQueryConditions().addCondition("shanchubiaoshi", EConditionOperator.EQULS, XtZidiancanshu.SHANCHUBIAOSHI_BUSHANCHU);
        systemDictQueryDTO.getQueryConditions().addCondition("fenzubianma", EConditionOperator.EQULS, fenzubianma);
        systemDictQueryDTO.getQueryConditions().addCondition("canshuzhi", EConditionOperator.EQULS, canshuzhi);

        //获取字典集合
        List<XtZidiancanshu> list = systemDictService.getSystemDictList(systemDictQueryDTO);

        if (!CollectionUtils.isEmpty(list) && list.size() == 1) {
            XtZidiancanshu zidiancanshu = list.get(0);
            result = zidiancanshu.getCanshumingcheng();
        }

        return result;
    }

    /**
     * 系统字典参数全局刷新
     *
     * @param
     * @return
     */
    @PostMapping("flush")
    @ApiOperation(value = "系统字典参数全局刷新")
    public ResultVO flushParam() {
        PushQueueUtil.pushFushCacheMsgInReids();
        return ResultVO.success();
    }

}
